#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include "bmp_parse.h"
#include "font_parse.h"
#include "comp.h"
#include "thinning.h"
#include "normalize.h"
#include "feature.h"
#include "template.h"

using namespace std;

void display(COMP_RECT &rect)
{
	for (int i = 0; i < rect.height; i++) {
		for (int j = 0; j < rect.width; j++) {
			cout << rect.rect[i * rect.width + j];
		}
		cout << endl;
	}
}

vector<TEMP> Temp;
void getTemp(void)
{
	COMP_RECT rect_font;
	for (char i = '0'; i <= '9'; i++) {
		char font[3];
		font[0] = i;
		font[1] = 0;
		incise_font(font, rect_font);
		COMP_RECT normal_rect, thinning_rect;
		normalize(rect_font, normal_rect, rect_font.width + 2, rect_font.height + 2, 1.0);
		thinning(normal_rect, thinning_rect);
		//if (i == '4' || i == '9')
		//	display(normal_rect);
		TEMP temp(thinning_rect, string(1, i));
		//FEATURE feat(thinning_rect);
		Temp.push_back(temp);
	}
	for (i = 'a'; i <= 'z'; i++) {
		char font[3];
		font[0] = i;
		font[1] = 0;
		incise_font(font, rect_font);
		COMP_RECT normal_rect, thinning_rect;
		normalize(rect_font, normal_rect, rect_font.width + 2, rect_font.height + 2, 1.0);
		thinning(normal_rect, thinning_rect);
		//if (i == '4' || i == '9')
		//	display(normal_rect);
		TEMP temp(thinning_rect, string(1, i));
		//FEATURE feat(thinning_rect);
		Temp.push_back(temp);
	}
	for (i = 'A'; i <= 'Z'; i++) {
		char font[3];
		font[0] = i;
		font[1] = 0;
		incise_font(font, rect_font);
		COMP_RECT normal_rect, thinning_rect;
		normalize(rect_font, normal_rect, rect_font.width + 2, rect_font.height + 2, 1.0);
		thinning(normal_rect, thinning_rect);
		//if (i == '4' || i == '9')
		//	display(normal_rect);
		TEMP temp(thinning_rect, string(1, i));
		//FEATURE feat(thinning_rect);
		Temp.push_back(temp);
	}
}

int FEATURE2::sub_num = 8;

int main(int argc, char **argv)
{
	COMP_RECT rect_bmp, rect_font;
	//char *file = "J:\\ulkv3learning\\ocr_project\\Debug\\template\\0-2.bmp";
	char *file = argv[1];
	incise_bmp(file, rect_bmp);
	COMP_RECT normal_rect, thinning_rect;
	normalize(rect_bmp, normal_rect, rect_bmp.width + 2, rect_bmp.height + 2, 1.0);
	thinning(normal_rect, thinning_rect);

	display(thinning_rect);
	cout << endl;
	LoadStore();
	getTemp();
	//incise_font("1", rect_font);
	double min = 10000000;
	size_t n = 0;
	cout << "----------------------" << endl;
	FEATURE3 obj(thinning_rect);
	for (size_t j = 0; j < obj.feature.size(); j++) {
		cout << obj.feature[j] << " ";
	}
	cout << endl;
	for (size_t i = 0; i < Temp.size(); i++) {
		double dist = Temp[i].get_distance(thinning_rect);
		if (min > dist) {
			n = i;
			min = dist;
		}
		//display(Temp[i].rect);
		//break;
	}
	cout << Temp[n].getValue() << endl;
	/*
	for (int i = 0; i < rect.height; i++) {
		for (int j = 0; j < rect.width; j++) {
			cout << rect.rect[i * rect.width + j];
		}
		cout << endl;
	}
	*/
	return 0;
}
